package cn.ycgogo.org.service.impl;

import cn.ycgogo.org.domain.Employee;
import cn.ycgogo.org.domain.Shop;
import cn.ycgogo.org.domain.ShopAuditLog;
import cn.ycgogo.org.mapper.EmployeeMapper;
import cn.ycgogo.org.mapper.ShopAuditLogMapper;
import cn.ycgogo.org.mapper.ShopMapper;
import cn.ycgogo.org.service.ShopService;
import cn.ycgogo.org.basic.servie.impl.BaseServiceImpl;
import cn.ycgogo.org.util.BaiduAuditUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    

    @Override
    public void settlement(Shop shop) {
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new RuntimeException("信息不能为空!");
        }

        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new RuntimeException("两次密码不一致!");
        }

        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if (dbShop!=null){
            throw new RuntimeException("当前店铺已经入驻过,请直接登录!");
        }

        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new RuntimeException("店铺名字不合格,请重新输入!");
        }

        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new RuntimeException("店铺Logo不合格,请重新选取!");
        }

        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);

        shop.setAdminId(admin.getId());
        shopMapper.save(shop);

        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    /**
     * 驳回事件逻辑
     * @param log
     * @throws MessagingException
     */
    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        shopMapper.updateState(4,log.getShopId());
        log.setState(4);
        log.setAuditId(350L);

        shopAuditLogMapper.save(log);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8" );
        helper.setFrom("1462058295@qq.com");
        helper.setSubject("店铺激活文件");
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://172.16.6.225:8081/#/register'>点击这里</a>修改入驻信息重新提交</h3>", true);
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        shopMapper.updateState(2,log.getShopId());
        log.setState(2);
        log.setAuditId(350L);

        shopAuditLogMapper.save(log);


        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1462058295@qq.com");
        helper.setSubject("店铺激活邮件");
        //根据关联信息Shop_Id查到对应的店铺对象 目的:三连查 日志->店铺->店铺店长 找到对应的店长邮箱
        Shop shop = shopMapper.loadById(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://172.16.6.225:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //根据日志对应的店铺信息AdminId找到对应的店长信息对象
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        //获取到对应店长对象后,将他的邮箱设置进去
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void activatedState(Long id) {
        shopMapper.updateState(3,id);
    }
}
